﻿using Single.AppCore;
using Single.YitId;
using SqlSugar;
using System.Security.Claims;

namespace Single.Sugar.Entity
{
    /// <summary>
    /// 抽象模型
    /// </summary>
    public abstract class BaseEntity
    {
        /// <summary>
        /// 默认主键
        /// </summary>
        [SugarColumn(IsPrimaryKey = true)]
        public long Id { get; set; }

        /// <summary>
        /// 创建用户Id
        /// </summary>
        [SugarColumn(IsOnlyIgnoreUpdate = true)]
        public long CreateUserId { get; set; }

        /// <summary>
        /// 创建用户名称
        /// </summary>
        [SugarColumn(IsOnlyIgnoreUpdate = true)]
        public string CreateUserName { get; set; }

        /// <summary>
        /// 创建时间
        /// </summary>
        [SugarColumn(InsertServerTime = true, IsOnlyIgnoreUpdate = true)]
        public DateTime CreateTime { get; set; }

        /// <summary>
        /// 更新用户Id
        /// </summary>
        public long UpdateUserId { get; set; }

        /// <summary>
        /// 更新用户名称
        /// </summary>
        public string UpdateUserName { get; set; }

        /// <summary>
        /// 更新时间
        /// </summary>
        [SugarColumn(InsertServerTime = true, UpdateServerTime = true)]
        public DateTime UpdateTime { get; set; }

        #region 功能

        private ClaimsPrincipal _claimsPrincipal;
        private bool _aoped = false;
        private const long _defaultUserId = 0;
        private const string _defaultUserName = "system";

        /// <summary>
        /// 设置用户信息
        /// </summary>
        /// <param name="claimsPrincipal"></param>
        public void SetClaimsPrincipal(ClaimsPrincipal claimsPrincipal)
        {
            _claimsPrincipal = claimsPrincipal;
        }

        /// <summary>
        /// 初始化编号
        /// </summary>
        public void InitId(bool isNewId = false)
        {
            if (Id == 0 || isNewId)
            {
                Id = YitIdHelper.NextId();
            }
        }

        public void DataAopInsert()
        {
            if (!_aoped)
            {
                InitId();

                if (_claimsPrincipal == null)
                {
                    _claimsPrincipal = GlobalApp.User;
                }

                if (_claimsPrincipal != null && _claimsPrincipal.Identity.IsAuthenticated)
                {
                    CreateUserId = _claimsPrincipal.GetUserId();
                    CreateUserName = _claimsPrincipal.GetName();
                    UpdateUserId = _claimsPrincipal.GetUserId();
                    UpdateUserName = _claimsPrincipal.GetName();
                }
                else
                {
                    CreateUserId = _defaultUserId;
                    CreateUserName = _defaultUserName;
                    UpdateUserId = _defaultUserId;
                    UpdateUserName = _defaultUserName;
                }

                CreateTime = DateTime.Now;
                UpdateTime = DateTime.Now;

                _aoped = true;
            }
        }

        public void DataAopUpdate()
        {
            if (!_aoped)
            {
                if (_claimsPrincipal == null)
                {
                    _claimsPrincipal = GlobalApp.User;
                }

                if (_claimsPrincipal != null && _claimsPrincipal.Identity.IsAuthenticated)
                {
                    UpdateUserId = _claimsPrincipal.GetUserId();
                    UpdateUserName = _claimsPrincipal.GetName();
                }
                else
                {
                    UpdateUserId = _defaultUserId;
                    UpdateUserName = _defaultUserName;
                }

                UpdateTime = DateTime.Now;

                _aoped = true;
            }
        }

        #endregion
    }
}